﻿function resetSrhByUpdate(){
    document.getElementById("txt_ProductNo").value = "";
    document.getElementById("txt_ProductName").value = "";
}
function resetFactor() {
    document.getElementById("txt_buyerName").value = "";
}
$(function() {
    $('#checkedAll_bfp').click(function() {
        var scope = $('#tb_bfp');
        if ($(this).attr('checked')) {
            $(':checkbox', scope).attr('checked', true);
        } else {
            $(':checkbox', scope).attr('checked', null);
        }
    });
});
function checkProductsByUpdate() {
    if ($("#hd_proScope").val() == "") {
        alert("未添加新商品！");
        $("#val_proScope").show();
        return false;
    }
    return true;
}
function checkPriceByUpdate(obj) {
    if (obj.value != "" && obj.value != "0") {
        var flag = IsPriceNumericByUpdate(obj.value);
        if (!flag)
            obj.value = "1";
    }
    else
        obj.value = "1";
}
function IsPriceNumericByUpdate(oNum) {
    var strP = /^[0-9]+([.]\d{1,2})?$/;
    if (strP.test(oNum))
        return true;
    else
        return false;
}

function addTobfpItemByUpdate(obj, productId, unit) {
    if ($('#div_AddProduct').style = "display:none") {
        $("#div_AddProduct").css("display", "block");
    }

    var products = [];
    if ($('#hd_proScope').val().length > 0) {
        products = jsonParse($('#hd_proScope').val());
    }
    var isExists = false;

    $(products).each(function(entryIndex, entry) {
        if (entry['productId'] == productId) {
            isExists = true;
            return false;
        }
    });

    var productName = $.trim($(obj).parent().prev().prev().prev().prev().text());
    var sellerPrice = $.trim($($(obj).parent().prev().prev().prev().find('input')[0]).val());
    var price = $($(obj).parent().prev().prev().find('input')[0]).val();
    var sellerId = $($(obj).parent().prev().find('select')[0]).val();
    var sellerName = $($(obj).parent().prev().find('option:selected')[0]).text();
    if (!isExists) {
        var product = { 'productId': productId, 'unit': unit, 'productName': productName, 'price': price, 'sellerId': sellerId, 'sellerName': sellerName, 'sellerPrice': sellerPrice };
        products.push(product);
        $('#hd_proScope').val(Object.serialize2Str(products));
        updateTableRowByUpdate();
    }
    else {
        alert('您选择的商品已经存在！');
        return false;
    }
}
function updateTableRowByUpdate() {

    var products = [];
    if ($('#hd_proScope').val().length > 0) {
        products = jsonParse($('#hd_proScope').val());
        var sw = [];
        for (var i = 0; i < products.length; i++) {
            sw.push('<tr align="center" style="height: 20px;">');
            sw.push('<td bgcolor="#FFFFFF">' + (i + 1) + '</td>');
            sw.push('<td bgcolor="#FFFFFF"><a target="_blank" href="../user_productDetail_' + products[i]['productId'] + '.html">' + products[i]['productName'] + '</a></td>');
            sw.push('<td bgcolor="#FFFFFF"><span class="red">' + products[i]['sellerPrice'] + '</span></td>');
            sw.push('<td bgcolor="#FFFFFF"><span class="red">' + products[i]['price'] + '</span></td>');
            sw.push('<td bgcolor="#FFFFFF">' + products[i]['sellerName'] + '</td>');
            sw.push('<td bgcolor="#FFFFFF"><a href="javascript:void(0);" onclick="delFromHiddenValueByUpdate(' + products[i]['productId'] + ');">删除</a></td>');
            sw.push('</tr>');
        }
        $('#tableBody').empty();
        $('#tableBody').append(sw.join(''));
    }
}
function delFromHiddenValueByUpdate(productId) {
    var products = [];
    if ($('#hd_proScope').val().length > 0) {
        products = jsonParse($('#hd_proScope').val());
        for (var i = 0; i < products.length; i++) {
            if (products[i]["productId"] == productId) {
                products.remove(i);
                break;
            }
        }
        $('#hd_proScope').val(Object.serialize2Str(products));
        updateTableRowByUpdate();
    }
}